package com.geek.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

public class FileContextUtil {
	public static String loadContextString(String filePath) {
		File file = new File(filePath);
		if (file.exists()) {
			return loadContextString(file);
		}
		return null;
	}

	public static String loadContextString(File file) {
		StringBuffer configStr = new StringBuffer();
		BufferedReader br = null;
		String tempStr;
		try {
			br = new BufferedReader(new FileReader(file));
			tempStr = br.readLine();
			while (tempStr != null) {
				configStr.append(tempStr + "\r");
				tempStr = br.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return configStr.toString();
	}

	public static String readLastLines(File file, long rownum) throws IOException {
		if (!file.exists() || file.isDirectory() || !file.canRead()) {
			return null;
		}
		StringBuffer resultSbr = new StringBuffer();
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(file, "rw");
			long index = raf.length();
			String tempStr = getLineStr(raf, index);
			while (rownum > 0 && tempStr != null) {
				if (!"".equals(tempStr)) {
					resultSbr = new StringBuffer(tempStr).append("\r").append(resultSbr);
				}
				rownum--;
				index = index - tempStr.getBytes().length;
				tempStr = getLineStr(raf, index);
			}
		} finally {
			if (raf != null) {
				try {
					raf.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
		return resultSbr.toString();
	}

	private static String getLineStr(RandomAccessFile raf, long index) throws IOException {
		long pos = index - 1;
		String returnStr = "";
		if (pos > 0) {
			raf.seek(pos);
			if (raf.readByte() == '\r') {
				pos--;
			}
			raf.seek(pos);
			if (raf.readByte() == '\n') {
				pos--;
			}
			while (pos > 0) {
				pos--;
				raf.seek(pos);
				if (raf.readByte() == '\r') {
					break;
				}
				raf.seek(pos);
				if (raf.readByte() == '\n') {
					break;
				}
			}
			returnStr = raf.readLine();
		}

		return returnStr;
	}

	public static String loadContextString(InputStream istream) {
		StringBuffer configStr = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(istream));
		String tempStr;
		try {
			tempStr = br.readLine();
			while (tempStr != null) {
				configStr.append(tempStr + "\r\n");
				tempStr = br.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return configStr.toString();
	}
}
